package com.zhong.gang.app.ui.examine.price;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.google.gson.Gson;
import com.luck.picture.lib.tools.ToastUtils;
import com.zhong.gang.app.R;
import com.zhong.gang.app.databinding.ActivityPriceBinding;
import com.zhong.gang.app.ui.examine.adpter.ExamineProgressAdapter;
import com.zhong.gang.app.ui.examine.adpter.modle.ExamineProgressBean;
import com.zhong.gang.app.ui.examine.adpter.PriceAdapter;
import com.zhong.gang.app.ui.examine.adpter.modle.PriceBean;
import com.zhong.gang.app.ui.examine.adpter.modle.PriceProgressAdapterBean;
import com.zhong.gang.app.ui.examine.adpter.modle.PriceProgressBean;
import com.zhong.gang.app.ui.examine.presenter.ExaminePresenter;
import com.zhong.gang.applib.BaseApp;
import com.zhong.gang.applib.base.act.ActBaseRefresh;
import com.zhong.gang.applib.http.BaseDataBean;
import com.zhong.gang.applib.http.callback.DataCallBack;
import com.zhong.gang.applib.utils.ActivityController;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import java.util.ArrayList;
import java.util.List;

public class PriceAct extends ActBaseRefresh<ActivityPriceBinding, ExamineProgressAdapter> {
    private PriceAdapter priceAdapter;
    private ExaminePresenter presenter;
    private static final String ID = "ID";
    public static final void openAct(Context context,int id){
        Intent intent = new Intent(context,PriceAct.class);
        intent.putExtra(ID,id);
        context.startActivity(intent);
    }
    @Override
    public int LayoutRes() {
        return R.layout.activity_price;
    }

    @NonNull
    @Override
    public String CreateTitle() {
        return "价格审核";
    }

    @Override
    public void initView() {
        bind.setAct(this);
        bind.contentRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        bind.titleRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        initrefrsh(bind.pullto,new ExamineProgressAdapter(),bind.contentRecyclerView);
        bind.titleRecyclerView.setAdapter(priceAdapter = new PriceAdapter());
        presenter = new ExaminePresenter();
        getData();
    }

    @Override
    public void getData() {
        super.getData();
        int id = getIntent().getIntExtra(ID,0);
        presenter.getDetail(this,id,new DataCallBack<BaseDataBean<PriceBean>>(this){
            @Override
            public void onResponse(BaseDataBean<PriceBean> data) throws Exception {
                super.onResponse(data);
                bind.sn.setText("："+data.getResult().getSn());

                presenter.getMaterialTypePrice(PriceAct.this,data.getResult().getMaterialTypePriceId()
                        ,new DataCallBack(PriceAct.this){
                            @Override
                            public void onResponse(String response, int id) {
                                try {
                                    JSONObject object = new JSONObject(response);
                                    int code = object.getInt("code");
                                    if (code == 5026){
                                        startActivity(new Intent(PriceAct.this,BaseApp.loginAct));
                                        ActivityController.getInstance().closeActNot(BaseApp.loginAct);
                                    }else if (code == 200){
                                        List<PriceProgressAdapterBean> data = new ArrayList<>();
                                        String jsonStr = object.getString("result"); //json字符串
                                        Object json = new JSONTokener(jsonStr).nextValue();
                                        if(json instanceof JSONObject){
                                            getJsonArray(data,jsonStr);
                                        }else if (json instanceof JSONArray){
                                            JSONArray jsonArray = (JSONArray)json;
                                            for (int n = 0 ;n < jsonArray.length();n++){
                                                getJsonArray(data,jsonArray.getString(n));
                                                priceAdapter.setNewData(data);
                                            }
                                        }
                                        priceAdapter.setNewData(data);
                                    }else {
                                        ToastUtils.s(PriceAct.this,object.getString("message"));
                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        });
                bind.okButton.setVisibility(data.getResult().getStatus() == 0?View.VISIBLE:View.GONE);
                bind.notOkButton.setVisibility(data.getResult().getStatus() == 0?View.VISIBLE:View.GONE);
            }
        });
        presenter.recordList(this,id,new ActCallBack<BaseDataBean<List<ExamineProgressBean>>>(){
            @Override
            public void onResponse(BaseDataBean<List<ExamineProgressBean>> data) throws Exception {
                super.onResponse(data);
                setData(data.getResult());
            }
        });
    }

    private void getJsonArray(List<PriceProgressAdapterBean> data, String str){
        PriceProgressBean priceBean = new Gson().fromJson(str,PriceProgressBean.class);
        bind.gcLc.setText("："+priceBean.getMaterialYardName());
        if (!priceBean.isMaterialTypeType()){
            PriceProgressAdapterBean bean = new PriceProgressAdapterBean();
            bean.setTitle(priceBean.getMaterialTypeName());
            bean.setTypeName("料场价格");
            bean.sethPrice(priceBean.getMaterialYardPrice());
            bean.setqPrice(priceBean.getOldMaterialTypePrice().getMaterialYardPrice());
            data.add(bean);
        }else {
            PriceProgressAdapterBean oldBean = new PriceProgressAdapterBean();
            oldBean.setTitle(priceBean.getMaterialTypeName());
            oldBean.setTypeName("起运价");
            oldBean.sethPrice(priceBean.getShippingPrice());
            oldBean.setqPrice(priceBean.getOldMaterialTypePrice().getShippingPrice());


            PriceProgressAdapterBean newBean = new PriceProgressAdapterBean();
            newBean.setTitle(priceBean.getMaterialTypeName());
            newBean.setTypeName("到厂价");
            newBean.sethPrice(priceBean.getSteelMillPrice());
            newBean.setqPrice(priceBean.getOldMaterialTypePrice().getSteelMillPrice());
            data.add(oldBean);
            data.add(newBean);
        }

    }



    public void onViewClicked(View view){
        switch (view.getId()){
            case R.id.ok_button:
                presenter.auditExamine(this,getIntent().getIntExtra(ID,0),2);
                break;
            case R.id.not_ok_button:
                presenter.auditExamine(this,getIntent().getIntExtra(ID,0),1);
                break;
        }
    }
}
